package config

import (
	"fmt"
	"github.com/BurntSushi/toml"
	"sync"
)

type MysqlConfig struct {
	Dsn             string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime int
	PrintSql        bool
}

type LogConfig struct {
	LogBase     string
	LogLevel    int
	Debug       bool
	LogToStdout bool
	RollSize    int
}

type RedisConfig struct {
	//for conn
	Network  string
	Addr     string
	Password string
	DB       int

	DialTimeout  int
	ReadTimeout  int
	WriteTimeout int
	//for pool
	PoolSize int
}

type WxConfig struct {
	AppId    string
	Secret   string
	OfferId  string
	MidasKey string
	PF       string
}

type Config struct {
	Mysql         MysqlConfig
	LogConfig     *LogConfig
	Redis         *RedisConfig
	ListenAddr    string
	CookieTimeOut int
}

var instance *Config
var instanceOne sync.Once

func InitConfig(configPath string) *Config {
	instanceOne.Do(func() {
		instance = &Config{}
		instance.newConfig(configPath)
	})
	return instance
}

func (c *Config) newConfig(configPath string) {
	_, err := toml.DecodeFile(configPath, c)
	if err != nil {
		panic(fmt.Sprintf("decode conf file %s %s", configPath, err))
	}
}

func GetConfig() *Config {
	return instance
}
